const FILE_TODOS 				= "todo.dat";

function Todo( todoID, todoListID, todoTitle, todoDescripton, todoStart, todoEnd, todoDuration, todoPriority )
{
	this.ID 			= todoID || unique_id( );
	this.listID 		= todoListID || null;
	this.title 			= todoTitle || '';
	this.description 	= todoDescripton || '';
	this.startTime		= todoStart || Time.now( );
	this.endTime		= todoEnd || 0;
	this.duration		= todoDuration || 0;
	this.priority		= todoPriority || -1;
	
	this.saveToFile = function( )
	{
		Log.debug( 'saving todo Id'+ this.ID + ' to file' );
		var isNew = true;
		var file = IO.file_get_contents( FILE_TODOS ).split( NL );
		for ( var i = 0; i < file.length; i++ )
		{
			if( file[i].indexOf( this.ID ) != -1 )
			{
				file[i] = this.toString( );
				isNew = false;	
				break;
			}
		}
		if( isNew )
		{
			file.push( this.toString( ) );
		}
		return IO.file_put_contents( file.join( NL ), FILE_TODOS );
	}
	
	this.toString = function( )
	{
//		Log.debug( 'saving before: '+ this.description );
//		Log.debug( 'saving after: '+ nl2newline( this.description ) );
		
		var str = this.ID + DELIMITER;
    	str += this.listID + DELIMITER;
    	str += this.title + DELIMITER;
    	str += nl2newline( this.description ) + DELIMITER;
    	str += this.startTime + DELIMITER;
    	str += this.endTime + DELIMITER;
    	str += this.duration + DELIMITER;
    	str += this.priority;
    	return str;
	}
	
	return this;
}

function tdt_getTodoFromArray( params )
{
	Log.dump( params );
//	Log.debug( 'before: '+ params[3] );
	params[3] =  newline2nl( params[3] );
//	Log.debug( 'after: '+ params[3] );
	return new Todo( params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7] );
}

function tdt_getTodoFromString( str )
{
	var params = str.split( DELIMITER );
	params.map( trim );
	return tdt_getTodoFromArray( params );
}

function tdt_getTodoById( id )
{
	if( ! id > 0 )
	{
		Log.error( 'Invalid id.' );
		return null;
	}
	var file = IO.file_get_contents( FILE_TODOS ).split( NL );
	for ( var i = 0; i < file.length; i++ )
	{
		var line = file[i];
		if( line.indexOf( id ) != -1 )
		{
			return tdt_getTodoFromString( line );
		}
	}
	Log.error( 'The todo couldn`t be found.' );
}

function tdt_getTodos( listID )
{
	Log.debug( "Trying to get todos from "+ listID );
	var aTodos = [];
	var file = IO.file_get_contents( FILE_TODOS ).split( NL );
	
	for ( var i = 0; i < file.length; i++ )
	{
		var line = file[i];
		if( line == '' ) continue;

		var list = line.split( DELIMITER );
		if( typeof listID == 'undefined' || list[1] != listID )
		{
			continue;
		}
		aTodos.push( tdt_getTodoFromString( line ) );
	}
	return aTodos;
}

function tdt_editTodo( oTodo )
{
	var file = IO.file_get_contents( FILE_TODOS ).split( NL );
	for ( var i = 0; i < file.length; i++ )
	{
		var line = file[i];
		if( line.indexOf( oTodo.ID ) != -1 )
		{
//			Log.debug( 'set edit content before: '+ file.join( NL ) );
			file[i] = oTodo.toString( );
//			Log.debug( 'set edit content after: '+ file.join( NL ) );
			return IO.file_put_contents( file.join( NL ), FILE_TODOS );
		}
	}
}

function tdt_deleteTodoById( id )
{
	Log.debug( 'Delete todo id: '+ id +";" );
	var file = IO.file_get_contents( FILE_TODOS ).split( NL );
	for ( var i = 0; i < file.length; i++ )
	{
		var line = file[i];
		if( line.indexOf( id ) != -1 )
		{	
			file.splice( i, 1 );
//			Log.debug( 'set del content '+ file.join( NL ) );
			return IO.file_put_contents( file.join( NL ), FILE_TODOS );
		}
	}
}